package com.ihrm.modules.sys.controller;

import com.ihrm.common.utils.PageUtils;
import com.ihrm.common.utils.R;
import com.ihrm.modules.sys.entity.DistrictEntity;
import com.ihrm.modules.sys.service.DistrictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author gaogzhen
 * @email gaogzhen@foxmail.com
 * @date 2020-11-26 22:32:49
 */
@RestController
@RequestMapping("sys/district")
@Api(tags = "地区")
public class DistrictController {
    @Autowired
    private DistrictService districtService;

    /**
     * 列表
     */
    @GetMapping
    @ApiOperation("列表")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = districtService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询")
    public R info(@PathVariable("id") Integer id) {
        DistrictEntity district = districtService.getById(id);

        return R.ok().put("district", district);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation("添加")
    public R save(@RequestBody DistrictEntity district) {

        districtService.save(district);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public R update(@RequestBody DistrictEntity district) {
        // ValidatorUtils.validateEntity(district);
        districtService.updateById(district);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @ApiOperation("删除")
    public R delete(@RequestBody Integer[] ids) {
        districtService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据层级查询地区
     * @param level 层级
     * @return  层级地区数据
     */
    @GetMapping("/level/{level}")
    @ApiOperation("查询层级地区数据")
    public R level(@PathVariable Integer level) {
        List<DistrictEntity> districts = districtService.level(level);
        return R.ok().put("districts", districts);
    }

}
